<?php

	function main($time_ini,$time_end) { 
	?>
	<table width="100%"  border="0" cellpadding="4" cellspacing="1">
	   <tr bgcolor="#24869B">
          <td colspan="3">Totais entre <?php echo $time_ini;?> e <?php echo $time_end;?></td>
		</tr>
    <?php
	$QViewDays2 = "SELECT *
	FROM exchange_operations 
	WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' AS firsttime
	GROUP BY Time";
	$ViewDays2 = $GLOBALS['CONN']->db_array($QViewDays2); 
	for ($vi = 1; $vi <= $ViewDays2[0]; $vi++) { 
	
	?>
        <tr bgcolor="#F2F2F2">
		<form action="index.php" method="get">
          <td>Data:
                <?php echo $ViewDays2[$vi]['Time'];?>
          </td>
          <td>Total:
		  
		  </td>
          <td>
		  <?php
		  $time_ini2 = $ViewDays2[$vi]['Time'];
		  $time_end2 = $ViewDays2[$vi]['Time'];
		  ?>
		  <input type="hidden" value="<?php echo $time_ini2;?>" name="time_ini2">
		  <input type="hidden" value="<?php echo $time_end2;?>" name="time_end2">
		  <input type="hidden" value="Days" name="View">
		  <input name="Detalhes" type="submit" id="Detalhes" value="Lojas">		  </td></form>
        </tr>
        <?php
		} ?>
</table>
	<?php }
	
	function days($time_ini,$time_end,$CSS) { ?>
	<table width="100%"  border="0" cellpadding="4" cellspacing="1">
	 	<tr bgcolor="#24869B">
          <td colspan="3">Lista de valores por loja</td>
      </tr>
	<tr bgcolor="#C9D7E0"><td width="20%" bgcolor="#C9D7E0">Lojas</td>
	<td width="40%">Totais</td>
	<td width="40%"><?php if ($CSS['PrintClass'] != "Noforms") { ?>Ver Detalhes<?php } ?></td>
	</tr>
	 <form action="index.php" method="get">
		 <input type="hidden" value="<?php echo $time_ini;?>" name="time_ini">
		  <input type="hidden" value="<?php echo $time_end;?>" name="time_end">
        <?php
	$QCallMainLoja = "SELECT * FROM exchange_operations
	WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}'
	GROUP BY main_office_ref";
	
	$CallMainLoja = $GLOBALS['CONN']->db_array($QCallMainLoja);
	for ($v = 1; $v <= $CallMainLoja[0]; $v++) { 
	 ?>
        <tr bgcolor="#F3F5F8">
          <td valign="top">
	<?php
	$QCallSubLoja = "SELECT * FROM main_offices
	WHERE ref = '{$CallMainLoja[$v]['main_office_ref']}'";
	
	$CallSubLoja = $GLOBALS['CONN']->db_array($QCallSubLoja);
	for ($s = 1; $s <= $CallSubLoja[0]; $s++) { 
 
	echo $CallSubLoja[$s]['office_number'];?><b> | </b><?php echo $CallSubLoja[$s]['name'];
		  
	if ($CSS['PrintClass'] != "Noforms") { 
		   } 
	 } ?>
    <?php echo $CSS['lines'];?>
</td>
          <td valign="top">
	<?php 
	//TOTAL LOJA
	
	$QCallMain = "SELECT * FROM exchange_operations WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
	AND main_office_ref = '{$CallMainLoja[$v]['main_office_ref']}' GROUP BY type_ref";
	$CallMain = $GLOBALS['CONN']->db_array($QCallMain);
	for ($m = 1; $m <= $CallMain[0]; $m++) { 
	$Total = 0;
	$OpDifer = 0;

	if ($m == 1) {$condition = 'out_currency_ref';} elseif ($m == 2) {$condition = 'in_currency_ref';} 
	if ($m == 1) {$condition2 = 'in_currency_ref';} elseif ($m == 2) {$condition2 = 'out_currency_ref';} 
	$QCallSub = "SELECT * FROM exchange_operations 
	WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
	AND type_ref = '{$CallMain[$m]['type_ref']}'
	AND main_office_ref = '{$CallMainLoja[$v]['main_office_ref']}'
	GROUP BY in_currency_ref, out_currency_ref";
	$CallSub = $GLOBALS['CONN']->db_array($QCallSub);
	for ($n = 1; $n <= $CallSub[0]; $n++) { 


			$QCallTotalAmount = "SELECT SUM(acerto) as acerto, SUM(Amount) AS Amount, SUM(payed) AS payed, tax1, Time, in_currency_ref, out_currency_ref FROM exchange_operations
			WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
			AND type_ref = '{$CallMain[$m]['type_ref']}' 
			AND {$condition2} = '{$CallSub[$n][$condition2]}'
			AND status IN (2,6)
			AND main_office_ref = '{$CallMainLoja[$v]['main_office_ref']}'
			GROUP BY tax1";
			$CallTotalAmount = $GLOBALS['CONN']->db_array($QCallTotalAmount);
			for($x = 1; $x <= $CallTotalAmount[0]; $x++) {
			// Cacula Totais
		if ($m == 1 && $condition == "in_currency_ref") {$taxcalc = "buy1";}
		elseif ($m == 1 && $condition2 == "in_currency_ref") {$taxcalc = "buy2";}
		elseif ($m == 2 && $condition == "in_currency_ref") {$taxcalc = "sell1";}
		elseif ($m == 2 && $condition2 == "in_currency_ref") {$taxcalc = "sell2";}
		
		if ($condition == "in_currency_ref") {$regcalc = $CallTotalAmount[$x]['in_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['out_currency_ref'];
		}
		elseif ($condition == "out_currency_ref") {$regcalc = $CallTotalAmount[$x]['out_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['in_currency_ref'];
		}
		$QCallTot = "SELECT * FROM exchange_rates
			WHERE rate_time <= '{$CallTotalAmount[$x]['Time']}'
			AND (ref_currency1 = '{$regcalc}' AND ref_currency2 = '{$regcalc2}')
			OR (ref_currency2 = '{$regcalc}' AND ref_currency1 = '{$regcalc2}')
			ORDER BY rate_time DESC LIMIT 0,1";
			$CallTot = $GLOBALS['CONN']->db_array($QCallTot);
	 $Total = $Total+$CallTotalAmount[$x]['payed'];
	 $OpDifer = $OpDifer+$CallTotalAmount[$x]['acerto'];
	 $Toi = $Total-$OpDifer;
	 $OpDif = $OpDifer;
	 ?>
	<span class="style6">Entrou</span> a <?php echo $CallTotalAmount[$x]['tax1'];?> : 

<?php echo $CallTotalAmount[$x]['Amount'].$__sysCurrency[$CallTotalAmount[$x]['in_currency_ref']]." ".$__sysCurrency[$CallTotalAmount[$x]['in_currency_ref']];?>
	<br>
	<span class="style67">Saiu</span> a <?php echo $CallTotalAmount[$x]['tax1'];?> : 

<?php echo $CallTotalAmount[$x]['acerto'].$__sysCurrency[$CallTotalAmount[$x]['out_currency_ref']]."

".$__sysCurrency[$CallTotalAmount[$x]['out_currency_ref']];?>
	<hr align="left" width="100">
	<?php }
	} 
}
	echo $CSS['lines'];?></td>
          <td valign="top"><?php if ($CSS['PrintClass'] != "Noforms") { ?>
		  <input name="office[]" type="checkbox" value="<?php echo $CallMainLoja[$v]['main_office_ref'];?>">
		<?php } ?>
        <?php echo $CSS['lines'];?>
</td>
       </tr>
        <?php } ?>
		<tr><td></td><td></td><td><?php if ($CSS['PrintClass'] != "Noforms") { ?>
		<input name="View" type="submit" value="Detalhes"> | <input name="View" type="submit" value="Relat�rio 

de Totais">
		<?php } ?></td></tr>
	  </form>
</table>
<?php	
	}
	
	function offices($time_ini,$time_end,$V,$office,$CSS) { 
	?>
	<table width="100%"  border="0" cellpadding="4" cellspacing="1">
      <tr bgcolor="#24869B">
          <td colspan="2"><?php 
		  if($V == "B" && $CSS['PrintClass'] != "Noforms") { echo "<form accept-charset='utf-8' action=\"index.php\" method=\"get\">";
		 echo "Data:".$GLOBALS['T']." <input type=\"hidden\" value=\"{$time_ini}\" name=\"time_ini\">
		  <input type=\"hidden\" value=\"{$time_end}\" name=\"time_end\">
		  <input type=\"hidden\" value=\"{$office}\" name=\"office\">
		  <input type=\"hidden\" value=\"Details\" name=\"View\">
		  <input  name=\"Details\" type=\"submit\" value=\"Detalhes das Opera��es\">
		  </form>";
		  
		  }?></td>
      </tr>
	 
	<?php 
	// VISTA POR LOJA | TOTAIS 
	
	$QCallMain = "SELECT * FROM exchange_operations 
	WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
	AND main_office_ref = '{$office}' 
	AND status IN (2,6)
	GROUP BY type_ref";
	$CallMain = $GLOBALS['CONN']->db_array($QCallMain);
	for ($m = 1; $m <= $CallMain[0]; $m++) { 
	$Total = 0;
	$OpDifer = 0;
	?>
			<tr bgcolor="#A9BFCF">
          <td colspan="2" valign="top">
		  <?php if ($CallMain[$m]['type_ref'] == '1') { echo "Compra"; } elseif ($CallMain[$m]['type_ref'] == 

'2') { echo "Venda";} ?></td>
      </tr>
	
<?php
	if ($m == 1) {$condition = 'out_currency_ref';} elseif ($m == 2) {$condition = 'in_currency_ref';} 
	if ($m == 1) {$condition2 = 'in_currency_ref';} elseif ($m == 2) {$condition2 = 'out_currency_ref';} 
	
	$QCallSub = "SELECT * FROM exchange_operations 
	WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
	AND main_office_ref = '{$office}'
	AND type_ref = '{$CallMain[$m]['type_ref']}' 
	AND status IN (2,6)
	GROUP BY {$condition2}";
	$CallSub = $GLOBALS['CONN']->db_array($QCallSub);
	for ($n = 1; $n <= $CallSub[0]; $n++) { ?>
		<tr bgcolor="#F2F2F2">
          <td valign="bottom" bgcolor="#F3F5F8"><?php echo $__sysCurrency[$CallSub[$n][$condition2]]."

".$__sysCurrency[$CallSub[$n][$condition2]].$CSS['lines'];?></td>
    <td width="60%" bgcolor="#F3F5F8">
	<?php
			$QCallTotalAmount = "SELECT SUM(acerto) as acerto, SUM(Amount) AS Amount, SUM(payed) AS payed, 

tax1, Time, in_currency_ref, out_currency_ref FROM exchange_operations
			WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
			AND type_ref = '{$CallMain[$m]['type_ref']}' 
			AND main_office_ref = '{$office}'
			AND {$condition2} = '{$CallSub[$n][$condition2]}'
			AND status IN (2,6)
			GROUP BY tax1";
			$CallTotalAmount = $GLOBALS['CONN']->db_array($QCallTotalAmount);
			for($x = 1; $x <= $CallTotalAmount[0]; $x++) {
			// Cacula Totais
		if ($m == 1 && $condition == "in_currency_ref") {$taxcalc = "buy1";}
		elseif ($m == 1 && $condition2 == "in_currency_ref") {$taxcalc = "buy2";}
		elseif ($m == 2 && $condition == "in_currency_ref") {$taxcalc = "sell1";}
		elseif ($m == 2 && $condition2 == "in_currency_ref") {$taxcalc = "sell2";}
		
		if ($condition == "in_currency_ref") {$regcalc = $CallTotalAmount[$x]['in_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['out_currency_ref'];
		}
		elseif ($condition == "out_currency_ref") {$regcalc = $CallTotalAmount[$x]['out_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['in_currency_ref'];
		}
		$QCallTot = "SELECT * FROM exchange_rates
			WHERE rate_time <= '{$CallTotalAmount[$x]['Time']}'
			AND (ref_currency1 = '{$regcalc}' AND ref_currency2 = '{$regcalc2}')
			OR (ref_currency2 = '{$regcalc}' AND ref_currency1 = '{$regcalc2}')
			ORDER BY rate_time DESC LIMIT 0,1";
			$CallTot = $GLOBALS['CONN']->db_array($QCallTot);
	 $Total = $Total+$CallTotalAmount[$x]['payed'];
	 $OpDifer = $OpDifer+$CallTotalAmount[$x]['acerto'];
	 $Toi = $Total-$OpDifer;
	 $OpDif = $OpDifer;
	 ?>	 Entrou a
<?php echo $CallTotalAmount[$x]['tax1'];?> : 

<?php echo $CallTotalAmount[$x]['Amount'].$__sysCurrency[$CallTotalAmount[$x]['in_currency_ref']]."

".$__sysCurrency[$CallTotalAmount[$x]['in_currency_ref']];?>
	<br>
	Saiu a <?php echo $CallTotalAmount[$x]['tax1'];?> : 

<?php echo $CallTotalAmount[$x]['acerto'].$__sysCurrency[$CallTotalAmount[$x]['out_currency_ref']]."

".$__sysCurrency[$CallTotalAmount[$x]['out_currency_ref']];?>

	<?php }
	 ?>
	<tr bgcolor="#F3F5F8">
          <td colspan="8" valign="top">
	<b>Total: <?php echo number_format($OpDif,2).$__sysCurrency[$CallMain[$m][$condition]]."

".$__sysCurrency[$CallMain[$m][$condition]];?>
    </b><?php echo $CSS['lines'];?>	| Diferen&ccedil;a Operacional:	<?php echo number_format($Toi,2);
	$Total = 0;
	$OpDifer = 0; ?></td>
      </tr>
	<?php 
	} 
}
	?>
</table>
<?php }
	//ALL DETAILS
	function alldetails($time_ini,$time_end,$CSS) { 
	 $Total = 0;
	 $OpDifer = 0;
	 $Toi = $Total-$OpDifer = 0;
	 $OpDif  = 0;
	 $condition = 0;
	?>
	
	<table width="100%"  border="0" cellpadding="4" cellspacing="1">
	  <tr bgcolor="#24869B">
          <td colspan="9">Detalhes de Opera&ccedil;&otilde;es | Entre <?php echo $time_ini." e ".$time_end;?></td>
      </tr>

	<?php
	// Chama Datas e Lojas
	$TotalTOTAL = 0;
	$MovimentosTOTAL = 0;
	$OpDifer = 0;
	$dayantigo = 0;
	$QCallDaysOrder = $GLOBALS['CONN']->db_array("SELECT *, DATE(Time) AS Time FROM exchange_operations
	WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
	AND status > 1 ORDER BY Time ASC");
	for ($t = 1; $t <= $QCallDaysOrder[0]; $t++) { 
	$daystart = $QCallDaysOrder[$t]['Time'];

	if ($daystart != $dayantigo) {
	$dayantigo = $daystart;
	$QCallGroupOrder = "SELECT * FROM exchange_operations
	WHERE DATE(Time) = '{$daystart}' AND
	status IN (2,6)
	GROUP BY main_office_ref ORDER BY main_office_ref ASC";
	
	$CallGroupOrder = $GLOBALS['CONN']->db_array($QCallGroupOrder);
	for ($e = 1; $e <= $CallGroupOrder[0]; $e++) { ?>
	<tr bgcolor="#A9BFCF"><td colspan="9"><b><?php if ($e == 1) {
	echo $daystart;
	} ?></b></td>
	</tr>
		
	<tr bgcolor="#C9D7E0">
          <td colspan="7" valign="top">
    <?php 
	$OfficeList = $GLOBALS['CONN']->db_array("SELECT * FROM main_offices WHERE ref = '{$CallGroupOrder[$e]['main_office_ref']}'");
	
	echo $OfficeList[1]['name']." | ".$OfficeList[1]['office_number']." | ".$OfficeList[1]['address_city'].$CSS['lines'];?>          </td>
          <td valign="top">
	  <tr bgcolor="#C9D7E0">
          <td valign="top">Hora</td>
          <td valign="top">Recibo</td>
          <td valign="top">Estado</td>
          <td valign="top" bgcolor="#C9D7E0">Tipo</td>
          <td valign="top">Cliente</td>
          <td valign="top" bgcolor="#C9D7E0">Taxa</td>
          <td valign="top">Valor Recebido </td>
          <td valign="top">Valor Entregue </td>
      </tr>
		<?php
	$CallMainDetalhes = $GLOBALS['CONN']->db_array("SELECT * FROM exchange_operations
	WHERE DATE(Time) = '{$daystart}' AND main_office_ref = '{$OfficeList[1]['ref']}' 
	ORDER BY Time,control_ref ASC");

	 
	$Movimentos = count($CallMainDetalhes)-1;
	$MovimentosTOTAL = $MovimentosTOTAL+$Movimentos;
	for ($m = 1; $m <= $CallMainDetalhes[0]; $m++) { ?>
        <tr <?php 
		if ($CallMainDetalhes[$m]['status'] == 5) { echo "bgcolor=\"#EEDCB7\""; }  else { echo "bgcolor=\"#F3F5F8\""; } ?>>
          <td valign="top">
            <?php echo $CallMainDetalhes[$m]['Time'].$CSS['lines'];?>			</td>
          <td valign="top">
    <?php
	
	$Office = $GLOBALS['CONN']->db_array("SELECT * FROM main_offices WHERE ref = '{$CallMainDetalhes[$m]['main_office_ref']}'");
			$Office[1]['office_number'].$CallMainDetalhes[$m]['control_ref'].$CSS['lines'];?>			</td>
          <td valign="top"><?php if ($CallMainDetalhes[$m]['status'] == 2) { echo "PAGO";} 

elseif($CallMainDetalhes[$m]['status'] == 5) { echo "CANCELADO";}?></td>
          <td valign="top"><?php if($CallMainDetalhes[$m]['type_ref'] == "1") { echo "Compra";} else {
			echo "Venda";} ?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top">
	<?php
	$QCallCli = "SELECT * FROM main_clients WHERE ref = '{$CallMainDetalhes[$m]['main_clients_ref']}'";
	$Cli = $GLOBALS['CONN']->db_array($QCallCli);
	echo $Cli[1]['client_number'].$CSS['lines'];?></td>
          <td valign="top"><?php echo $CallMainDetalhes[$m]['tax1'].$CSS['lines'];?></td>
          <td 

valign="top"><?php echo $CallMainDetalhes[$m]['Amount'].$__sysCurrency[$CallMainDetalhes[$m]['in_currency_ref']].$CSS['lines'];?></td>
          <td 

valign="top"><?php echo $CallMainDetalhes[$m]['acerto'].$__sysCurrency[$CallMainDetalhes[$m]['out_currency_ref']].$CSS['lines'];?></td>
      </tr>
        <?php } 
	// VISTA POR LOJA | TOTAIS 
	
	$QCallMain = "SELECT * FROM exchange_operations WHERE DATE(Time) = '{$daystart}'
	AND main_office_ref = '{$OfficeList[1]['ref']}' 
	AND status IN (2,6)
	GROUP BY type_ref";
	$CallMain = $GLOBALS['CONN']->db_array($QCallMain);
	for ($m = 1; $m <= $CallMain[0]; $m++) { 
	$Total = 0;

	if ($m == 1) {$condition = 'out_currency_ref';} elseif ($m == 2) {$condition = 'in_currency_ref';} 
	if ($m == 1) {$condition2 = 'in_currency_ref';} elseif ($m == 2) {$condition2 = 'out_currency_ref';} 

	$QCallSub = "SELECT * FROM exchange_operations 
	WHERE DATE(Time) = '{$daystart}'
	AND main_office_ref = '{$OfficeList[1]['ref']}'
	AND type_ref = '{$CallMain[$m]['type_ref']}' 
	AND status IN (2,6)
	GROUP BY {$condition2}";
	$CallSub = $GLOBALS['CONN']->db_array($QCallSub);
	for ($n = 1; $n <= $CallSub[0]; $n++) { ?>
		<tr bgcolor="#F3F5F8">
          <td colspan="5" valign="bottom" bgcolor="#F3F5F8"><?php echo $__sysCurrency[$CallSub[$n][$condition2]]."

".$__sysCurrency[$CallSub[$n][$condition2]].$CSS['lines'];?></td>
    <td width="60%" colspan="4">
	<?php
			$QCallTotalAmount = "SELECT SUM(acerto) as acerto, SUM(Amount) AS Amount, SUM(payed) AS payed, 

tax1, Time, in_currency_ref, out_currency_ref FROM exchange_operations
			WHERE DATE(Time) = '{$daystart}'
			AND type_ref = '{$CallMain[$m]['type_ref']}' 
			AND main_office_ref = '{$OfficeList[1]['ref']}'
			AND {$condition2} = '{$CallSub[$n][$condition2]}'
			AND status IN (2,6)
			GROUP BY tax1";
			$CallTotalAmount = $GLOBALS['CONN']->db_array($QCallTotalAmount);
			for($x = 1; $x <= $CallTotalAmount[0]; $x++) {
			// Cacula Totais
		if ($m == 1 && $condition == "in_currency_ref") {$taxcalc = "buy1";}
		elseif ($m == 1 && $condition2 == "in_currency_ref") {$taxcalc = "buy2";}
		elseif ($m == 2 && $condition == "in_currency_ref") {$taxcalc = "sell1";}
		elseif ($m == 2 && $condition2 == "in_currency_ref") {$taxcalc = "sell2";}
		
		if ($condition == "in_currency_ref") {$regcalc = $CallTotalAmount[$x]['in_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['out_currency_ref'];
		}
		elseif ($condition == "out_currency_ref") {$regcalc = $CallTotalAmount[$x]['out_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['in_currency_ref'];
		}
		$QCallTot = "SELECT * FROM exchange_rates
			WHERE rate_time <= '{$CallTotalAmount[$x]['Time']}'
			AND (ref_currency1 = '{$regcalc}' AND ref_currency2 = '{$regcalc2}')
			OR (ref_currency2 = '{$regcalc}' AND ref_currency1 = '{$regcalc2}')
			ORDER BY rate_time DESC LIMIT 0,1";
			$CallTot = $GLOBALS['CONN']->db_array($QCallTot);
	 $Total = $Total+$CallTotalAmount[$x]['payed'];
	 $OpDifer = $OpDifer+$CallTotalAmount[$x]['acerto'];
	 $Toi = $Total-$OpDifer;
	 $OpDif = $OpDifer;
	 $TotalTOTAL = $Total+$TotalTOTAL;
	 ?>
	 <span class="style6">Entrou</span> a <?php echo $CallTotalAmount[$x]['tax1'];?> : 

<?php echo $CallTotalAmount[$x]['Amount'].$__sysCurrency[$CallTotalAmount[$x]['in_currency_ref']]."

".$__sysCurrency[$CallTotalAmount[$x]['in_currency_ref']];?>
	<br>
	<span class="style67">Saiu</span> a <?php echo $CallTotalAmount[$x]['tax1'];?> : 

<?php echo $CallTotalAmount[$x]['acerto'].$__sysCurrency[$CallTotalAmount[$x]['out_currency_ref']]."

".$__sysCurrency[$CallTotalAmount[$x]['out_currency_ref']];
} ?>
    <?php echo $CSS['lines'];?></td>
      </tr>
	<?php 
	} ?>
	<tr bgcolor="#F3F5F8">
          <td colspan="9" valign="top">
	<b>Total: <?php echo number_format($OpDif,2).$__sysCurrency[$CallMain[$m][$condition]]."

".$__sysCurrency[$CallMain[$m][$condition]];?>
    </b><?php echo $CSS['lines'];?>	| Diferen&ccedil;a Operacional:	<?php echo number_format($Toi,2);
	$Total = 0;
	$OpDifer = 0; ?></td>
      </tr>
	<?php }
	?>
		<tr bgcolor="#F3F5F8">
          <td colspan="9" valign="top">
	<b>N�mero de Movimentos: <?php echo $Movimentos;?>
		  </td>
      </tr>
	<?php
		}
	 } 
	 } ?>
	 <tr bgcolor="#24869B">
          <td colspan="9" valign="top">
	<b>VALORES GERAIS</b></td>
      </tr>
	 <tr bgcolor="#A9BFCF">
          <td colspan="9" valign="top">
	<b>N�mero de Movimentos: <?php
	if (isset($CallMain)) {
	echo $MovimentosTOTAL;?> | Total: <?php echo number_format($TotalTOTAL,2).$__sysCurrency[$CallMain[1][$condition]]." ".$__sysCurrency[$CallMain[1][$condition]];
	} ?>
    </b><?php echo $CSS['lines'];?>				 
		  </td>
      </tr>
	 <tr bgcolor="#F3F5F8">
       <td colspan="9" valign="top">
	     <?php if ($CSS['lines'] == "") { ?>
		    <p><strong>Concilia&ccedil;&atilde;o Banc&aacute;ria
            </strong>
         <table width="100%" border="0" cellpadding="4" cellspacing="1">
	   <?php
	   	$QOffices = "SELECT ref,name FROM main_offices WHERE (ref_domain = '1' OR office_number = '800-E') AND 

office_number != '822' ORDER BY name ASC";
		$Offices = $GLOBALS['CONN']->db_array($QOffices);
		for ($p = 1; $p <= $Offices[0]; $p++) { ?>
		
		<tr><td bgcolor="#C9D7E0" colspan="2"><?php echo $Offices[$p]['name'];?></td>
		<tr>
	   <?php
			$QTotalOfficeIN = "SELECT 
			SUM(Amount) AS Amount,
			in_currency_ref
			FROM exchange_operations
			WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
			AND status IN (2,6)
			AND main_office_ref = '{$Offices[$p]['ref']}'
			GROUP BY in_currency_ref";
			$TotalOfficeIN = $GLOBALS['CONN']->db_array($QTotalOfficeIN);
			
			
			
			for ($o = 1; $o <= $TotalOfficeIN[0]; $o++) {
			
			$QTotalOfficeOUT = "SELECT 
			SUM(acerto) AS acerto,
			out_currency_ref
			FROM exchange_operations
			WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
			AND status IN (2,6)
			AND main_office_ref = '{$Offices[$p]['ref']}'
			AND out_currency_ref = '{$TotalOfficeIN[$o]['in_currency_ref']}'
			GROUP BY out_currency_ref";
			$TotalOfficeOUT = $GLOBALS['CONN']->db_array($QTotalOfficeOUT).$CSS['lines'];?><td></p> 
                <?php
			$IN = 0;
			$OUT = 0;

				$IN = $IN+$TotalOfficeIN[$o]['Amount'];
				if (isset($TotalOfficeOUT[1]['acerto'])) { $OUT = $OUT+$TotalOfficeOUT[1]['acerto'];} 

else {
				 $OUT = $OUT+0;}
				$Curr = $TotalOfficeIN[$o]['in_currency_ref'];
			
			if ($Curr == "1") {
				$Balance = $IN;
				} else {
				$Balance = $IN-$OUT;
				}
				echo "<b>A Depositar: ".$__sysCurrency[$Curr,'icon')."

".$__sysCurrency[$Curr,'name').": ".$Balance;?></b>
		 </td>
            <td>Entrou: <?php echo $IN;?> Saiu <?php echo $OUT;?></td>
         </tr>
                <?php 
					}} ?>
		 </table>
		<table width="100%" border="0" cellpadding="4" cellspacing="1">
          <tr>
            <td bgcolor="#C9D7E0" colspan="2"><strong>TOTAL</strong></td>
          <tr>
	   <?php
			$QTotalOfficeIN = "SELECT 
			SUM(Amount) AS Amount,
			in_currency_ref
			FROM exchange_operations
			WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
			AND status IN (2,6)
			GROUP BY in_currency_ref";
			$TotalOfficeIN = $GLOBALS['CONN']->db_array($QTotalOfficeIN);
			
			
			
			for ($o = 1; $o <= $TotalOfficeIN[0]; $o++) {
			
			$QTotalOfficeOUT = "SELECT 
			SUM(acerto) AS acerto,
			out_currency_ref
			FROM exchange_operations
			WHERE DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
			AND status IN (2,6)
			AND out_currency_ref = '{$TotalOfficeIN[$o]['in_currency_ref']}'
			GROUP BY out_currency_ref";
			$TotalOfficeOUT = $GLOBALS['CONN']->db_array($QTotalOfficeOUT).$CSS['lines'];?><td></p> 
                <?php
			$IN = 0;
			$OUT = 0;

				$IN = $IN+$TotalOfficeIN[$o]['Amount'];
				if (isset($TotalOfficeOUT[1]['acerto'])) { $OUT = $OUT+$TotalOfficeOUT[1]['acerto'];} 

else {
				 $OUT = $OUT+0;}
				$Curr = $TotalOfficeIN[$o]['in_currency_ref'];
			
			if ($Curr == "1") {
				$Balance = $IN;
				} else {
				$Balance = $IN-$OUT;
				}
				echo "<b>A Depositar: ".$__sysCurrency[$Curr,'icon')."

".$__sysCurrency[$Curr,'name').": ".$Balance;?></b>
		 </td>
            <td>Entrou: <?php echo $IN;?> Saiu <?php echo $OUT;?></td>
         </tr>
                <?php 
					}?>
       </table>
	   <?php } ?></td>
      </tr>
	<?php 
	//END ALL DETAILS
	 ?>
</table>
	<?php 
		}
	function details($time_ini,$time_end,$office,$CSS) { ?>
	
	<table width="100%"  border="0" cellpadding="4" cellspacing="1">
 	<?php
	$QCallSubLoja = "SELECT * FROM main_offices
	WHERE ref = '{$office}'";
	$CallSubLoja = $GLOBALS['CONN']->db_array($QCallSubLoja); ?>
	  <tr bgcolor="#24869B">
          <td colspan="8">Detalhes de Opera&ccedil;&otilde;es | Loja: <?php echo $CallSubLoja[1]['address_city'];?></td>
    </tr>
	<tr bgcolor="#C9D7E0">
          <td valign="top">Data</td>
          <td valign="top">Recibo</td>
          <td valign="top">Estado</td>
          <td valign="top">Tipo</td>
          <td valign="top">Cliente</td>
          <td valign="top">Taxa</td>
          <td valign="top">Valor Recebido </td>
          <td valign="top">Valor Entregue </td>
	</tr>
	<?php
	$QCallMainDetalhes = "SELECT * FROM exchange_operations
	INNER JOIN main_clients ON main_clients.ref = exchange_operations.main_clients_ref
	WHERE main_office_ref = '{$office}'
	AND DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}'";
	
	$CallMainDetalhes = $GLOBALS['CONN']->db_array($QCallMainDetalhes);
	for ($m = 1; $m <= $CallMainDetalhes[0]; $m++) { ?>
        <tr <?php if ($CallMainDetalhes[$m]['status'] == 5) { echo "bgcolor=\"#EEDCB7\""; }  else { echo "bgcolor=\"#F3F5F8\""; } ?>>
          <td valign="top">
            <?php echo $CallMainDetalhes[$m]['Time'].$CSS['lines'];?></td>
          <td valign="top">
            <?php echo $CallSubLoja[1]['office_number'].$CallMainDetalhes[$m]['control_ref'].$CSS['lines'];?></td>
          <td valign="top"><?php if ($CallMainDetalhes[$m]['status'] == 2) { echo "PAGO";} 

elseif($CallMainDetalhes[$m]['status'] == 5) { echo "CANCELADO";}?></td>
          <td valign="top"><?php if($CallMainDetalhes[$m]['type_ref'] == "1") { echo "Compra";} else {echo "Venda";} 

?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php echo $CallMainDetalhes[$m]['client_number'].$CSS['lines'];?></td>
          <td valign="top"><?php echo $CallMainDetalhes[$m]['tax1'].$CSS['lines'];?></td>
          <td 

valign="top"><?php echo $CallMainDetalhes[$m]['Amount'].$__sysCurrency[$CallMainDetalhes[$m]['in_currency_ref']].$CSS['lines'];?></td>
          <td 

valign="top"><?php echo $CallMainDetalhes[$m]['acerto'].$__sysCurrency[$CallMainDetalhes[$m]['out_currency_ref']].$CSS['lines'];?></td>
        </tr>
        <?php }
	
	$QCallMainDetalhes = "SELECT *, AVG(tax1) AS tax1, SUM(Amount) AS Amount, SUM(acerto) AS acerto FROM 

exchange_operations
	WHERE main_office_ref = '{$office}'
	AND status IN (2,6)
	AND DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
	GROUP BY type_ref,tax1,out_currency_ref,in_currency_ref";
	
	$CallMainDetalhes = $GLOBALS['CONN']->db_array($QCallMainDetalhes);
	for ($m = 1; $m <= $CallMainDetalhes[0]; $m++) { ?>
        <tr class="boxsub">
          <td valign="top" colspan="5">Totais <?php if($CallMainDetalhes[$m]['type_ref'] == "1") { echo "Compra";} 

else {echo "Venda";} ?>:
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php echo number_format($CallMainDetalhes[$m]['tax1'],4).$CSS['lines'];?></td>
          <td 

valign="top"><?php echo $CallMainDetalhes[$m]['Amount'].$__sysCurrency[$CallMainDetalhes[$m]['in_currency_ref']].$CSS['lines'];?></td>
          <td 

valign="top"><?php echo $CallMainDetalhes[$m]['acerto'].$__sysCurrency[$CallMainDetalhes[$m]['out_currency_ref']].$CSS['lines'];?></td>
        </tr>
        <?php } ?>
</table>
<?php 
}

	function officetotals($time_ini,$time_end,$office,$CSS) { ?>
	
	<table width="100%"  border="0" cellpadding="4" cellspacing="1">
 	<?php
	$QCallSubLoja = "SELECT * FROM main_offices
	WHERE ref = '{$office}'";
	$CallSubLoja = $GLOBALS['CONN']->db_array($QCallSubLoja); ?>
	  <tr bgcolor="#24869B">
          <td colspan="4">Total de Opera&ccedil;&otilde;es | Loja: <?php echo $CallSubLoja[1]['address_city'];?></td>
    </tr>
	<tr bgcolor="#C9D7E0">
          <td valign="top">&nbsp;</td>
          <td valign="top">Taxa</td>
          <td valign="top">Valor Recebido </td>
          <td valign="top">Valor Entregue </td>
	</tr>
	<?php
	$QCallMainDetalhes = "SELECT *, AVG(tax1) AS tax1, SUM(Amount) AS Amount, SUM(acerto) AS acerto FROM 

exchange_operations
	WHERE main_office_ref = '{$office}'
	AND status IN (2,6)
	AND DATE(Time) BETWEEN '{$time_ini}' AND '{$time_end}' 
	GROUP BY tax1, in_currency_ref, in_currency_ref, type_ref";
	
	$CallMainDetalhes = $GLOBALS['CONN']->db_array($QCallMainDetalhes);
	for ($m = 1; $m <= $CallMainDetalhes[0]; $m++) { ?>
        <tr  bgcolor="#F3F5F8">
          <td valign="top">Tipo            <?php if($CallMainDetalhes[$m]['type_ref'] == "1") { echo "Compra";} else 

{echo "Venda";} ?>:
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php echo number_format($CallMainDetalhes[$m]['tax1'],4).$CSS['lines'];?></td>
          <td 

valign="top"><?php echo $CallMainDetalhes[$m]['Amount'].$__sysCurrency[$CallMainDetalhes[$m]['in_currency_ref']].$CSS['lines'];?></td>
          <td 

valign="top"><?php echo $CallMainDetalhes[$m]['acerto'].$__sysCurrency[$CallMainDetalhes[$m]['out_currency_ref']].$CSS['lines'];?></td>
        </tr>
        <?php } ?>
</table>
<?php 
} ?>